package com.guangfeng.yql.security.aspect;

import com.guangfeng.yql.security.config.WhiteConfig;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

@Slf4j
public class UrlAspect {

    @Autowired
    WhiteConfig whiteConfig;

    @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public Object requestMapping(ProceedingJoinPoint joinPoint) {
        Method method = getMethod(joinPoint);
        RequestMapping annotation = getAnnotation(method, RequestMapping.class);

        return null;
    }

    @Around("@annotation(org.springframework.web.bind.annotation.PostMapping)")
    public Object postMapping(ProceedingJoinPoint joinPoint) {
        return null;
    }

    @Around("@annotation(org.springframework.web.bind.annotation.GetMapping)")
    public Object getMapping(ProceedingJoinPoint joinPoint) {
        return null;
    }

    @Around("@annotation(org.springframework.web.bind.annotation.PutMapping)")
    public Object putMapping(ProceedingJoinPoint joinPoint) {
        return null;
    }

    @Around("@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
    public Object deleteMapping(ProceedingJoinPoint joinPoint) {
        return null;
    }


    public Method getMethod(ProceedingJoinPoint joinPoint) {
        Class<?> aClass = joinPoint.getTarget().getClass();
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            if (method.getName().equals(joinPoint.getSignature().getName())) {
                return method;
            }
        }
        return null;
    }

    public <T extends Annotation> T getAnnotation(Method method, Class<T> annotation) {
        return method.getAnnotation(annotation);
    }

}
